package com.order.web;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.order.dao.AduserQuery;
import com.order.dao.VegeEvaQuery;
import com.order.dao.bean.Aduser;
import com.order.dao.bean.VegeEvaluate;
import com.order.tools.util.HibernateUtil;

@WebServlet("/AduserUpdateServlet")
public class AduserUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public AduserUpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html;charset=gbk");
    	request.setCharacterEncoding("gbk");
    	String aduser_id = request.getParameter("id");
    	String action = request.getParameter("action");
    	Aduser aduser = AduserQuery.queryObj(aduser_id);
    	
    	if(action.equals("delete")){
    		
    		HibernateUtil.delete(aduser);
    		RequestDispatcher rd=request.getRequestDispatcher("/AduserServlet");
    		rd.forward(request,response);
    	}
    	else if(action.equals("update")){
    		
    		Aduser aduser1 = getObj(request);
    		HibernateUtil.update(aduser1);
       		RequestDispatcher rd=request.getRequestDispatcher("/AduserServlet");
            rd.forward(request,response);
    	}
    	else if(action.equals("getObj")){

    		request.setAttribute("aduser",aduser);	
       		RequestDispatcher rd=request.getRequestDispatcher("/updateAduser.jsp");
            rd.forward(request,response);
    		//System.out.println("aduser:"+aduser.getAduser_id());
    	}
    	else if(action.equals("add")){
    		
    		String maxId = AduserQuery.queryMaxId();
    		String nextId = getNextId(maxId);
    		Aduser aduser2 = getObj(request,nextId);
    		HibernateUtil.add(aduser2);
       		RequestDispatcher rd=request.getRequestDispatcher("/AduserServlet");
            rd.forward(request,response);
    	}
    	
	}
	protected Aduser getObj(HttpServletRequest request){

    	Aduser aduser = new Aduser();
    	aduser.setAduser_id(request.getParameter("aduser_id"));
    	aduser.setAduser_name(request.getParameter("aduser_name"));
    	aduser.setAduser_contact(request.getParameter("aduser_contact"));
    	aduser.setAduser_number(request.getParameter("aduser_number"));
    	aduser.setAduser_remarks(request.getParameter("aduser_remarks"));
    	return aduser;
	}
	protected Aduser getObj(HttpServletRequest request,String id){

    	Aduser aduser = new Aduser();
    	aduser.setAduser_id(id);
    	aduser.setAduser_name(request.getParameter("aduser_name"));
    	aduser.setAduser_contact(request.getParameter("aduser_contact"));
    	aduser.setAduser_number(request.getParameter("aduser_number"));
    	aduser.setAduser_remarks(request.getParameter("aduser_remarks"));
    	return aduser;
	}
	protected String getNextId(String maxId){
		
		String nextId;
		char temp = ' ';
		int count=maxId.length()-1;
		temp = maxId.charAt(count);
		while(temp<='9'&& temp>'0'){
			temp = maxId.charAt(--count);
		}
		int val = Integer.parseInt( maxId.substring((count+1)) );
		val++;
		nextId = maxId.substring(0, count+1)+val;
		
		return nextId;
	}

}
